package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.SchoolLinkman;
import cn.wolfcode.crm.domain.StudentTracking;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.SchoolLinkmanMapper;
import cn.wolfcode.crm.mapper.SchoolMapper;
import cn.wolfcode.crm.mapper.StudentTrackingMapper;
import cn.wolfcode.crm.query.StudentTrackingQueryObject;
import cn.wolfcode.crm.service.IStudentTrackingService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class StudentTrackingServiceImpl implements IStudentTrackingService {

    @Autowired
    private StudentTrackingMapper mapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private SchoolLinkmanMapper schoolLinkmanMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    public List<StudentTracking> selectAll() {
        return mapper.selectAll();
    }

    public void insert(StudentTracking studentTracking) {
        mapper.insert(studentTracking);
    }

    public void updateByPrimaryKey(StudentTracking studentTracking) {
        mapper.updateByPrimaryKey(studentTracking);
    }

    public void changState(Long id) {
        mapper.changState(id);
    }

    public PageResult query(StudentTrackingQueryObject qo) {
        int total = mapper.query4Count(qo);
        if (total > 0) {
            return new PageResult(total, mapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public void trackingSave(Client client,Date date) {
        StudentTracking st = new StudentTracking();
        st.setDate(date);
        //查询学校id
        Long schoolId = schoolMapper.selectById(client.getSchoolName());
        //根据学校id查询学校联系人
        SchoolLinkman linkman = schoolMapper.selectSchoolId(schoolId).getLinkmanContacts();
        //根据用户id查用户
        String username = employeeMapper.selectByPrimaryKey(client.getSalemen().getId()).getUsername();
        mapper.trackingSave(schoolId,client,st.getDate(),username,linkman);
    }

}
